package com.liyy.leetcode.editor.cn;

//题目编号:83
//题目标题:删除排序链表中的重复元素	
//题目标记:remove-duplicates-from-sorted-list	

//题目描述:
//给定一个排序链表，删除所有重复的元素，使得每个元素只出现一次。 
//
// 示例 1: 
//
// 输入: 1->1->2
//输出: 1->2
// 
//
// 示例 2: 
//
// 输入: 1->1->2->3->3
//输出: 1->2->3 
// Related Topics 链表
	
public class RemoveDuplicatesFromSortedList {
  
    public static void main(String[] args) {
       Solution solution = new RemoveDuplicatesFromSortedList().new Solution();
        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(1);
        ListNode l3 = new ListNode(2);
        ListNode l4 = new ListNode(3);
        ListNode l5 = new ListNode(3);
        l1.next = l2;
        l2.next = l3;
        l3.next = l4;
        l4.next = l5;

        ListNode curr = l1;
        System.out.print("原始链表内容");
        while (curr != null){
            System.out.print(curr.val);
            curr = curr.next;
        }

        //换行
        System.out.println("");

        //删除重复元素
        ListNode result = solution.deleteDuplicates(l1);

        System.out.print("删除重复元素后的链表内容");
        ListNode curr2 = result;
        while (curr2 != null){
            System.out.print(curr2.val);
            curr2 = curr2.next;
        }
    }
  
//leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for singly-linked list.
 * public class com.liyy.leetcode.editor.cn.ListNode {
 *     int val;
 *     com.liyy.leetcode.editor.cn.ListNode next;
 *     com.liyy.leetcode.editor.cn.ListNode(int x) { val = x; }
 * }
 */
class Solution {
    /**
     * 相等时跳过(删除)重复元素
     * @param head
     * @return
     */
    public ListNode deleteDuplicates(ListNode head) {
        ListNode curr = head;
        while (curr != null && curr.next != null) {
            if (curr.val == curr.next.val) {
                curr.next = curr.next.next;
            }else {
                curr = curr.next;
            }
        }

        return head;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}